﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using System.Xml;
using System.Xml.Linq;

namespace KPAR
{
    public static class XMLifier
    {
        public static void AssignNounGender(XElement doc)
        {
            
        }

        public static void AssignNounNumber(XElement doc)
        {
            List<string> sgPronouns = new List<string>(new string[] { "i", "me", "he", "him", "she", "her", 
                                                "it", "mine", "his", "hers", "its" });
            List<string> plPronouns = new List<string>(new string[] { "we", "us", "they", "them", "ours", "theirs" });
            //List<string> indiscPronouns = new List<string>(new string[] { "yours", "you" });

            IEnumerable<XElement> nounPhrases =
                from noun in doc.Descendants("NC")
                select noun;
            foreach (XElement nounPhrase in nounPhrases)
            {
                bool hasPluralToken = false;
                bool hasSingularToken = false;
                if (nounPhrase.Descendants("token").Count() > 1)
                {
                    foreach (XElement token in nounPhrase.Descendants("token"))
                    {
                        if (token.Value.ToLower() == "these" ||
                            token.Value.ToLower() == "those")
                        {
                            hasPluralToken = true;
                        }
                    }
                    XElement lastToken = nounPhrase.Descendants("token").Last();
                    if (lastToken.Attribute("pos").Value == "NNS" ||
                        lastToken.Attribute("pos").Value == "NPS")
                    {
                        hasPluralToken = true;
                    }
                    if (!hasPluralToken)
                    {
                        hasSingularToken = true;
                    }
                }
                else
                {
                    XElement singleToken = nounPhrase.Descendants("token").First();
                    if (singleToken.Attribute("pos").Value == "NN" ||
                        singleToken.Attribute("pos").Value == "NP")
                    {
                        hasSingularToken = true;
                    }
                    else if (singleToken.Attribute("pos").Value == "NNS" ||
                                singleToken.Attribute("pos").Value == "NPS")
                    {
                        hasPluralToken = true;
                    }
                    else if (sgPronouns.Contains(singleToken.Value.ToLower()))
                    {
                        hasSingularToken = true;
                    }
                    else if (plPronouns.Contains(singleToken.Value.ToLower()))
                    {
                        hasPluralToken = true;
                    }
                          
                }
                if (hasPluralToken && !hasSingularToken)
                {
                    nounPhrase.Attribute("number").Value = "pl";
                }
                else if (!hasPluralToken && hasSingularToken)
                {
                    nounPhrase.Attribute("number").Value = "sg";
                }
            }
        }

        public static void Run()
        {
            int sectionID = 0;
            int headingID = 0;
            int paragraphID = 0;
            int sentenceID = 0;

            //Console.WriteLine("pr0n");
            //int ncID = 0;
            //int pcID = 0;
            //int vcID = 0;
            //int prtID = 0;
            //int lstID = 0;
            //int intjID = 0;
            //int conjcID = 0;
            //int adjcID = 0;
            //int advcID = 0;

            int tokenID = 0;

            bool hasParagraph = false;
            bool belongsToHeading = false;
            bool newSentence = false;
            bool withinPC = false;

            using (TextFieldParser parser = new TextFieldParser(@"C:\Users\Christopher\Desktop\chunked_testlauf.txt", Encoding.GetEncoding("iso-8859-1")))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters("\t");

                using (XmlWriter writer = XmlWriter.Create(@"C:\Users\Christopher\Desktop\chunked_testlauf.xml", new XmlWriterSettings { Indent = true }))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("text");

                    writer.WriteStartElement("section");
                    writer.WriteAttributeString("id", "" + ++sectionID);

                    while (!parser.EndOfData)
                    {
                        string[] line = parser.ReadFields();

                        #region ChunkerElementHandling
                        if (line[0].StartsWith("<"))
                        {
                            if (line[0].StartsWith("</"))
                            {
                                if (line[0].StartsWith("</PC"))
                                {
                                    withinPC = false;
                                }
                                writer.WriteEndElement();
                            }
                            else
                            {
                                if (newSentence)
                                {
                                    writer.WriteStartElement("sentence");
                                    writer.WriteAttributeString("id", sectionID + ":" + paragraphID + ":" + ++sentenceID);
                                    newSentence = false;
                                }

                                tokenID++;

                                switch (line[0].Substring(1, line[0].Length - 2))
                                {
                                    case "CONJC":
                                        writer.WriteStartElement("CONJC");
                                        break;

                                    case "INTJ":
                                        writer.WriteStartElement("INTJ");
                                        break;

                                    case "LST":
                                        writer.WriteStartElement("LST");
                                        break;

                                    case "PRT":
                                        writer.WriteStartElement("PRT");
                                        break;

                                    case "VC":
                                        writer.WriteStartElement("VC");
                                        break;

                                    case "NC":
                                        writer.WriteStartElement("NC");
                                        writer.WriteAttributeString("number", "indisc");
                                        writer.WriteAttributeString("gender", "indisc");
                                        writer.WriteAttributeString("prepositional", withinPC.ToString());
                                        
                                        break;

                                    case "PC":
                                        withinPC = true;
                                        writer.WriteStartElement("PC");
                                        break;

                                    case "ADJC":
                                        writer.WriteStartElement("ADJC");
                                        break;

                                    case "ADVC":
                                        writer.WriteStartElement("ADVC");
                                        break;
                                }

                                if (belongsToHeading)
                                {
                                    writer.WriteAttributeString("id", sectionID + ":H:" + headingID + ":" + tokenID);
                                }
                                else
                                {
                                    writer.WriteAttributeString("id", sectionID + ":" + paragraphID + ":" + sentenceID + ":" + tokenID);
                                }
                            }
                        }
                        #endregion
                        else
                        {
                            switch (line[0])
                            {
                                case "~":
                                    #region HeadingHandling
                                    if (hasParagraph)
                                    {
                                        writer.WriteEndElement();
                                        hasParagraph = false;
                                        writer.WriteStartElement("section");
                                        writer.WriteAttributeString("id", "" + ++sectionID);
                                    }
                                    else
                                    {
                                        if (belongsToHeading)
                                        {
                                            writer.WriteEndElement();
                                        }
                                        belongsToHeading = true;
                                        writer.WriteStartElement("heading");
                                        writer.WriteAttributeString("id", sectionID + ":" + ++headingID);
                                    }
                                    #endregion
                                    break;

                                case "§":
                                    #region ParagraphHandling
                                    hasParagraph = true;
                                    belongsToHeading = false;
                                    writer.WriteEndElement();
                                    writer.WriteStartElement("paragraph");
                                    writer.WriteAttributeString("id", sectionID + ":" + ++paragraphID);
                                    writer.WriteStartElement("sentence");
                                    writer.WriteAttributeString("id", sectionID + ":" + paragraphID + ":" + ++sentenceID);
                                    newSentence = false;
                                    #endregion
                                    break;

                                default:
                                    if (newSentence)
                                    {
                                        writer.WriteStartElement("sentence");
                                        writer.WriteAttributeString("id", sectionID + ":" + paragraphID + ":" + ++sentenceID);
                                        newSentence = false;
                                    }
                                    writer.WriteStartElement("token");
                                    if (belongsToHeading)
                                    {
                                        writer.WriteAttributeString("id", sectionID + ":H:" + headingID + ":" + ++tokenID);
                                    }
                                    else
                                    {
                                        writer.WriteAttributeString("id", sectionID + ":" + paragraphID + ":" + sentenceID + ":" + ++tokenID);
                                    }
                                    writer.WriteAttributeString("pos", line[1]);
                                    writer.WriteAttributeString("lemma", line[2]);

                                    if (line[1].StartsWith("IN") || line[1].Equals("TO"))
                                        writer.WriteAttributeString("prepositional", withinPC.ToString());

                                    writer.WriteString(line[0]);
                                    writer.WriteEndElement();

                                    if (line[1].Equals("SENT"))
                                    {
                                        writer.WriteEndElement();
                                        newSentence = true;
                                    }
                                    break;
                            }
                        }
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
            }

            XElement doc = XElement.Load(@"C:\Users\Christopher\Desktop\chunked_testlauf.xml");
            AssignNounNumber(doc);
            doc.Save(@"C:\Users\Christopher\Desktop\chunked_testlauf.xml");


        }
    }
}
